<template>
  <div v-document-click="documentClick">
    <span class="name">按省份选择:</span>
    <m-select
      :list="provinceList"
      title="省份"
      :showWrapperActive="provinceActive"
      :value="province"
      className="province"
      @change_active="changeProvinceActive"
      @change="changeProvince"
    />
    <m-select
      :list="cityList"
      title="城市"
      :showWrapperActive="cityActive"
      :value="city"
      className="city"
      @change_active="changeCityActive"
      @change="changeCity"
      :disabled=cityDisabled
    />
    <span>直接搜索:</span>
    <el-select
      v-model="searchWord"
      filterable
      remote
      reserve-keyword
      placeholder="请输入关键词"
      :remote-method="remoteMethod"
      :loading="loading"
       @change="searchInto"
    >
      <el-option
        v-for="item in searchList"
        :key="item"
        :label="item"
        :value="item"
      ></el-option>
    </el-select>
  </div>
</template>
<script>
import MSelect from "./select.vue";
import api from "@/api/index.js";
export default {
  data() {
    return {
      showWrapperList: [""],
      provinceList: [],
      province: "省份",
      cityList: [],
      city: "城市",
      cityActive:false,
      provinceActive: false,
      cityDisabled:true,
      searchList: ["哈尔滨", "佳木斯", "牡丹江", "鹤岗"],
      searchWord: "",
      loading: false,
      cityDisabled: true,
    };
  },
  components: {
    MSelect,
  },
  created() {
    api.getProvinceList().then((res) => {
      this.provinceList = res.data.data.map((item) => {
        item.name = item.provinceName;
        return item;
      });
    });
  },
  methods: {
    documentClick() {
      this.provinceActive = false;
      this.cityActive = false;
    },
    remoteMethod(val) {
      // 直接搜索，请求后端接口
       if(!val){
        this.searchList=["哈尔滨", "佳木斯", "牡丹江", "鹤岗"];
        return;
      }
      this.searchList=this.searchList.filter((item)=>{
        if(item.match(val)){
          return item
        }
      })
     
    },
    changeProvince(item) {
      this.province = item.name;
      this.cityDisabled=false;
      this.cityList = item.cityInfoList;
      this.provinceActive = false;
    },
    changeProvinceActive(flag) {
      this.provinceActive = flag;
      if (flag) {
        this.cityActive = false;
      }
    },

    changeCityActive(flag) {
      this.provinceActive = false;
      if(this.cityDisabled){
        return;
      }
      this.cityActive = flag;
    },
    changeCity(item) {
      this.city = item.name;
      this.$store.dispatch("setPosition", item);
      this.$router.push({ name: "index" });
    },
    searchInto(item){
        this.$store.dispatch("setPosition", {name:item});
      this.$router.push({ name: "index" });
    }
  },
};
</script>
<style lang="scss">
@import "@/assets/css/changecity/iselect.scss";
</style>
